package com.smart.service;

import com.smart.dao.*;
import com.smart.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ForumService {

    @Autowired
    private TopicDao topicDao;

    @Autowired
    private BoardDao boardDao;

    @Autowired
    private PostDao postDao;

    @Autowired
    private UserDao userDao;

    //发表一个主题帖子，用户积分加10，论坛版块的主题帖子数加1
    public void addTopic(Topic topic) {
        Board board = (Board) boardDao.get(topic.getBoardId());
        board.setTopicNum(board.getTopicNum() + 1);
        topicDao.save(topic);
        // 创建主题帖子
        topic.getMainPost().setTopic(topic);
        MainPost post = topic.getMainPost();
        post.setCreateTime(new Date());
        post.setUser(topic.getUser());
        post.setPostTitle(topic.getTopicTitle());
        post.setBoardId(topic.getBoardId());
        // 持久化主题帖子
        postDao.save(post);

        //更新用户积分
        User user = topic.getUser();
        user.setCredit(user.getCredit() + 10);
        userDao.update(user);

    }

    //删除一个主题帖子，用户积分减，论坛版块的主题帖子数减一，删除主题帖子所有关联的帖子
    public void removeTopic(int topicId) {
        Topic topic = topicDao.get(topicId);

        //将论坛版块的主题帖子数减一
        Board board = boardDao.get(topic.getBoardId());
        board.setTopicNum(board.getTopicNum() - 1);
        boardDao.update(board);

        //发表该主题帖子的用户扣除50个积分
        User user = topic.getUser();
        user.setCredit(user.getCredit() - 50);
        userDao.update(user);

        //删除主题帖子及其关联的帖子
        topicDao.remove(topic);
        postDao.deleteTopicPosts(topicId);
    }

    // 添加一个回复帖子，用户积分加5，主题帖子回复数加一，并更新最后回复时间
    public void addPost(Post post) {
        postDao.save(post);
        User user = post.getUser();
        user.setCredit(user.getCredit() + 5);
        userDao.update(user);
//        Topic topic = post.getTopic();
//        topic.setReplies(topic.getReplies() + 1);
//        topic.setLastPost(new Date());
//        topicDao.update(topic);
        Topic topic = topicDao.get(post.getTopic().getTopicId());
        topic.setReplies(topic.getReplies() + 1);
        topic.setLastPost(new Date());
    }

    //删除一个回复的帖子，发表回复帖子的用户积分减20，主题帖的回复数减1
    public void removePost(int postId) {
        Post post = postDao.get(postId);
        postDao.remove(post);

        Topic topic = topicDao.get(post.getTopic().getTopicId());
        topic.setReplies(topic.getReplies() -1);

        User user = topic.getUser();
        user.setCredit(user.getCredit() - 20);
    }

    //创建论坛版块
    public void addBoard(Board board) {
        boardDao.save(board);
    }

    //删除一个版块
    public void removeBoard(int boardId) {
        Board board = boardDao.get(boardId);
        boardDao.remove(board);
    }


    //将帖子置为精华主贴贴
    public void makeDigestTopic(int topicId) {
        Topic topic = topicDao.get(topicId);
        topic.setDigest(Topic.DIGEST_TOPIC);
        User user = topic.getUser();
        user.setCredit(user.getCredit() + 100);
        //topic处于Hiber受管状态，无须显示更新
    }

    //获取所有的论坛版块
    public List<Board> getAllBoards() {
        return boardDao.loadAll();
    }

    //获取论坛版块某一页主题帖，以最后恢复时间降序排列
    public Page getPageTopics(int boardId, int pageNo, int pageSize) {
        return topicDao.getBoardDigestTopics(boardId, pageNo, pageSize);
    }

    //获取某同主题每一页帖子，以最后回复时间降序排列
    public Page getPagePosts(int topicID, int pageNo, int pageSize) {
        return postDao.getPagedPosts(topicID, pageNo, pageSize);
    }

    //查找出所有包括标题包含title的主题帖
    public Page queryTopicByTitle(String title, int pageNo, int pageSize) {
        return topicDao.queryTopicByTitle(title, pageNo, pageSize);
    }

    //根据boardId获取Board对象
    public Board getBoardById(int boardId) {
        return boardDao.get(boardId);
    }

    //根据topicId获取Topic对象
    public Topic getTopicByTopicId(int topicId) {
        return topicDao.get(topicId);
    }

    //根据回复帖子的对象
    public Post getPostByPostId(int postId) {
        return postDao.get(postId);
    }

    //将用户设为论坛版块的管理员
    public void addBoardManager(int boardId, String userName) {
        User user = userDao.getUserByUserName(userName);
        if (null == user) {
            throw new RuntimeException("用户名为" + userName + "的用户不存在。");
        } else {
            Board board = boardDao.get(boardId);
            user.getManBoards().add(board);
            userDao.update(user);
        }
    }

    //更改主题帖
    public void updateTopic(Topic topic) {
        topicDao.update(topic);
    }

    //更改回复帖子的内容
    public void updatePost(Post post) {
        postDao.update(post);
    }

}
